package com.lovetemperature.action;

import java.util.Date;
import java.util.List;

import com.lovetemperature.model.Cart;
import com.lovetemperature.model.GoodSet;
import com.lovetemperature.model.Member;
import com.lovetemperature.model.MemberState;
import com.lovetemperature.model.Order;
import com.lovetemperature.model.Stock;
import com.lovetemperature.service.OrderService;
import com.lovetemperature.service.ShopService;
import com.lovetemperature.service.StockService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CartAction extends ActionSupport{
	private String stockId;
	
	private OrderService orderService;
	private ShopService shopService;
	private StockService stockService;
	private Member member;
	
	private String phone;
	private String address;
	
	private Cart cart = new Cart();
	private List<GoodSet> curGoodSetList;
	
	private String message;
	
	public String addToCart(){
		member = (Member) ActionContext.getContext().getSession().get("user");
		if(checkMemberState()){
			Stock stock = stockService.findById(Long.parseLong(stockId));
			cart.addGood(stock);
			message = "添加成功!";
		}else{
			message = "请先激活会员";
		}
		return "json";
	}
	
	private boolean checkMemberState(){
		if(member.getState().equals(MemberState.已激活) || member.getState().equals(MemberState.银卡) || member.getState().equals(MemberState.金卡)){
			return true;
		}else{
			return false;
		}
	}
	
	public String showStepOne(){
		curGoodSetList = cart.getGoodSetList();
		return "success";
	}
	
	public String showStepTwo(){
		member = (Member) ActionContext.getContext().getSession().get("user");
		ActionContext.getContext().put("member", member);
		
		return "success";
	}
	
	public String submitCart(){
		List<Order> orderList = cart.generateOrderList();
		List<Stock> stockList = cart.generateStockList();
		
		int index;
		for(index=0;index<orderList.size();index++){
			Order order = orderList.get(index);
			Stock stock = stockList.get(index);
			
			//订单存档
			finishOrderInfo(order,stock);
			orderService.calculatePrice(order,member);
			orderService.addOrder(order);	
			
			//更新库存
			stockService.update(stock);
		}
		
		return "success";
	}

	private void finishOrderInfo(Order order,Stock stock) {
		order.setPickupDate(stock.getListDate());	//认为商品上架的日期 = 顾客取货日期
		order.setShop(stock.getShop());
		order.setCustomer(member);
		order.setPutOrderDate(new Date());
		order.setAddress(address);
		order.setPayNum("测试银行交易号");
		order.setPhone(phone);
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public String getStockId() {
		return stockId;
	}

	public void setStockId(String stockId) {
		this.stockId = stockId;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public List<GoodSet> getCurGoodSetList() {
		return curGoodSetList;
	}

	public void setCurGoodSetList(List<GoodSet> curGoodSetList) {
		this.curGoodSetList = curGoodSetList;
	}

	public String getMessage() {
		return message;
	}
}
